#!/bin/bash

# 构建项目
echo "Building project..."
npm run build

# 压缩dist目录
echo "Compressing dist folder..."
tar -czf dist.tar.gz dist/

# 上传到服务器
echo "Uploading to server..."
scp dist.tar.gz user@your-server:/tmp/

# SSH到服务器执行部署
echo "Deploying..."
ssh user@your-server << 'ENDSSH'
    # 备份当前版本
    if [ -d /var/www/html/dist ]; then
        mv /var/www/html/dist /var/www/html/dist_backup_$(date +%Y%m%d_%H%M%S)
    fi

    # 解压新版本
    cd /tmp
    tar -xzf dist.tar.gz
    mv dist /var/www/html/

    # 重启 Nginx
    sudo systemctl restart nginx

    # 清理
    rm dist.tar.gz
ENDSSH

echo "Deployment completed!" 